package com.zlp.config.restTemplate;

//import org.apache.http.conn.HttpClientConnectionManager;
//import org.apache.http.impl.client.HttpClientBuilder;
//import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
//import org.springframework.boot.web.client.RestTemplateBuilder;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.client.ClientHttpRequestFactory;
//import org.springframework.http.client.ClientHttpRequestInterceptor;
//import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.http.converter.StringHttpMessageConverter;
//import org.springframework.web.client.RestTemplate;
//
//import javax.annotation.Resource;
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//
//@Configuration
//public class RestTemplateConfig {
//
//    @Resource
//    private RestTemplateLog restTemplateLog;
//
//    @Bean
//    public RestTemplate restTemplate(RestTemplateBuilder builder) {
//        RestTemplate restTemplate = builder.build();
//        // 添加拦截器
//        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
//        interceptors.add(restTemplateLog);
//        restTemplate.setInterceptors(interceptors);
//
//        restTemplate.setRequestFactory(clientHttpRequestFactory());
//        // 使用 utf-8 编码集的 conver 替换默认的 conver（默认的 string conver 的编码集为"ISO-8859-1"）
//        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
//        Iterator<HttpMessageConverter<?>> iterator = messageConverters.iterator();
//        while (iterator.hasNext()) {
//            HttpMessageConverter<?> converter = iterator.next();
//            if (converter instanceof StringHttpMessageConverter) {
//                iterator.remove();
//            }
//        }
//        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
//
//        return restTemplate;
//    }
//
//
//    @Bean
//    public HttpClientConnectionManager poolingConnectionManager() {
//        PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
//        // 连接池最大连接数
//        poolingConnectionManager.setMaxTotal(10000);
//        // 每个主机的并发
//        poolingConnectionManager.setDefaultMaxPerRoute(10000);
//        return poolingConnectionManager;
//    }
//
//    @Bean
//    public HttpClientBuilder httpClientBuilder() {
//        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//        //设置HTTP连接管理器
//        httpClientBuilder.setConnectionManager(poolingConnectionManager());
//        return httpClientBuilder;
//    }
//
//    @Bean
//    public ClientHttpRequestFactory clientHttpRequestFactory() {
//        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
//        clientHttpRequestFactory.setHttpClient(httpClientBuilder().build());
//        // 连接超时，毫秒
//        clientHttpRequestFactory.setConnectTimeout(6000);
//        // 读写超时，毫秒
//        clientHttpRequestFactory.setReadTimeout(6000);
//        return clientHttpRequestFactory;
//    }
//}


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate配置
 * 这是一种JavaConfig的容器配置，用于spring容器的bean收集与注册，并通过参数传递的方式实现依赖注入。
 * "@Configuration"注解标注的配置类，都是spring容器配置类，springboot通过"@EnableAutoConfiguration"
 * 注解将所有标注了"@Configuration"注解的配置类，"一股脑儿"全部注入spring容器中。
 *
 * @author Zou.LiPing
 *
 */
@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(@Qualifier("simpleClientHttpRequestFactory") ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        // 获取数据超时时间 毫秒
        factory.setReadTimeout(5000);
        return factory;
    }

}